package com.ssb.plugins;

import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.ius.plugin.IusPluginUtil;
import com.apache.tools.DataMap;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;
import com.ssb.util.DateCalculationUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.*;

/**
 * @Classname SelectInsurProStatisticalPluginImpl
 * @Description 查询保险公司下产品月销量
 * @Date 2020/4/18 20:20
 * @Created by fpc
 */
public class SelectInsurProStatisticalPluginImpl implements CustomMethodPlugin {
    @Override
    public Object beforeInvoke(Map<String, Object> map) {
        String insurCode = String.valueOf(map.get("insurCode"));
        String groupType = String.valueOf(map.get("dateType"));
        String formatGroup = "%Y-%m";
        if("year".equals(groupType)){
            formatGroup = "%Y";
        }
        String startDate = StrUtil.doNull(String.valueOf(map.get("startDate")),"2020-02");
        String endDate = StrUtil.doNull(String.valueOf(map.get("startDate")), DateUtils.Now.fmt_yyyyMM());
        //获取查询月份
        List<String> monthList = DateCalculationUtil.getMonthBetween(startDate, endDate);
        //获取保险公司下的产品
        List<DataMap> proMapList = getProByInsurCode(insurCode);
        String column = " sio.pro_code AS procode,DATE_FORMAT(sio.insure_date,'"+formatGroup+"') AS yearmonth , count(sio.info_id*IFNULL(sio.number_of_units,1)) AS num";
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ");
        sb.append(column);
        sb.append(" FROM ss_insur_order sio ,ss_insur_pro sip WHERE sio.pro_code=sip.pro_code ");
        sb.append(" AND sip.insur_code='"+insurCode+"'");
        sb.append(" AND DATE_FORMAT(sio.insure_date, '%Y-%m') >='"+startDate+"'");
        sb.append(" AND DATE_FORMAT(sio.insure_date, '%Y-%m') <='"+endDate+"'");
        sb.append(" GROUP BY procode,yearmonth");
        List<DataMap> groupList = (List<DataMap>)IusPluginUtil.getInstance().select("ssb", sb.toString(), 0, 0);
        List<DataMap> list=null;
        if(!Validator.isEmpty(groupList)&&groupList.size()>0){
            for (DataMap pro:proMapList) {
                String proCode=String.valueOf(pro.get("proCode"));
                list = new ArrayList<>();
                for (DataMap data : groupList) {
                    if (data.get("procode").equals(proCode)) {
                        list.add(data);
                    }
                }
                pro.put("data",list);
            }
        }
        DataMap tempMap =null;
        for (DataMap pro:proMapList) {
            List<DataMap> dataList=(List<DataMap>)pro.get("data");
            for (String month:monthList) {
                boolean flag = true;
                if(!Validator.isEmpty(dataList)&&dataList.size()>0){
                    group:
                    for (DataMap data : dataList) {
                        if (data.get("yearmonth").equals(month)) {
                            flag = false;
                            break group;
                        }
                    }
                }
                if(flag){
                    tempMap = new DataMap();
                    tempMap.put("yearmonth",month);
                    tempMap.put("num",0);
                    tempMap.put("procode",pro.get("proCode"));
                    dataList.add(tempMap);
                }
            }
            Collections.sort(dataList, new Comparator<DataMap>() {
                @Override
                public int compare(DataMap o1, DataMap o2) {
                    String yearmonth1 =  String.valueOf(o1.get("yearmonth"));
                    String yearmonth2 =  String.valueOf(o2.get("yearmonth"));
                    return yearmonth1.compareTo(yearmonth2);
                }
            });
        }



        Map returnMap = new HashMap();
        returnMap.put(CustomMethodPlugin.pluginInnerReturn, proMapList);
        returnMap.put(CustomMethodPlugin.resultMark, "T");
        return returnMap;
    }

    @Override
    public Object doInvoke(Map<String, Object> map) {
        return null;
    }

    @Override
    public Object afterInvoke(Map<String, Object> map) {
        return null;
    }

    private List<DataMap> getProByInsurCode(String insurCode){
        List<DataMap> dataMapList = null;
        Map<String,Object> whereMap = new HashMap<>();
        whereMap.put("result_column","ssInsurPro.proCode,ssInsurPro.riskCname");
        whereMap.put("ssInsurPro.w_insurCode",insurCode);
        ResultEntity resultEntity = IusPluginUtil.getInstance().select("ssb", "list", "s_ssInsurPro", whereMap);
        if(!Validator.isEmpty(resultEntity)){
            dataMapList=(List<DataMap>)resultEntity.getEntity();
        }
        return dataMapList;
    }

    }
